package com.base.self.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.base.result.PageUtils;
import com.base.result.ResponseResult;
import com.base.result.ResponseResultUtils;
import com.base.self.common.SelfRuntimeException;
import com.base.self.dto.commodityCallPlan.CommodityCallPlanWebAddDTO;
import com.base.self.dto.commodityCallPlan.CommodityCallPlanWebDelDTO;
import com.base.self.dto.commodityCallPlan.CommodityCallPlanWebListDTO;
import com.base.self.dto.commodityCallPlan.CommodityCallPlanWebUpdateDTO;
import com.base.self.entity.CommodityCallPlan;
import com.base.self.mapper.CommodityCallPlanMapper;
import com.base.self.service.ICommodityCallPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * <p>
 * 话费礼包 服务实现类
 * </p>
 *
 * @author XiFYuW
 * @since 2020-11-18
 */
@Service
public class CommodityCallPlanServiceImpl extends ServiceImpl<CommodityCallPlanMapper, CommodityCallPlan> implements ICommodityCallPlanService {

    private final CommodityCallPlanMapper commodityCallPlanMapper;

    public CommodityCallPlanServiceImpl(CommodityCallPlanMapper commodityCallPlanMapper) {
        this.commodityCallPlanMapper = commodityCallPlanMapper;
    }

    @Override
    @Transactional(readOnly = true)
    public ResponseResult commodityCallPlanWebList(CommodityCallPlanWebListDTO commodityCallPlanWebListDTO) {
        final Map<String, Object> data = PageUtils.getDateMap(() -> page(PageUtils.getPage(
                new Page<>(), commodityCallPlanWebListDTO.getOffset(), commodityCallPlanWebListDTO.getLimit()),
                new LambdaQueryWrapper<CommodityCallPlan>()
                        .eq(CommodityCallPlan::getIsDel, 0)
                        .orderByDesc(CommodityCallPlan::getCreateTime)));
        return ResponseResultUtils.getResponseResultS("查询成功", data);
    }

    @Override
    @Transactional
    public ResponseResult commodityCallPlanWebAdd(CommodityCallPlanWebAddDTO commodityCallPlanWebAddDTO) {
        final CommodityCallPlan commodityCallPlan = CommodityCallPlan
                .builder()
                .createTime(DateUtil.date())
                .planName(commodityCallPlanWebAddDTO.getPlanName())
                .planPrice(commodityCallPlanWebAddDTO.getPlanPrice())
                .planPic(commodityCallPlanWebAddDTO.getPlanPic())
                .build();
        save(commodityCallPlan);
        return ResponseResultUtils.getResponseResultS("添加成功");
    }

    @Override
    @Transactional
    public ResponseResult commodityCallPlanWebUpdate(CommodityCallPlanWebUpdateDTO commodityCallPlanWebUpdateDTO) {
        final CommodityCallPlan commodityCallPlan = commodityCallPlanMapper.selectById(commodityCallPlanWebUpdateDTO.getId());
        Optional.ofNullable(commodityCallPlan).orElseThrow(() -> new SelfRuntimeException(ResponseResultUtils.getResponseResultF("话费礼包错误")));
        commodityCallPlan.setUpdateTime(DateUtil.date());
        commodityCallPlan.setPlanName(commodityCallPlanWebUpdateDTO.getPlanName());
        commodityCallPlan.setPlanPic(commodityCallPlanWebUpdateDTO.getPlanPic());
        commodityCallPlan.setPlanPrice(commodityCallPlanWebUpdateDTO.getPlanPrice());
        updateById(commodityCallPlan);
        return ResponseResultUtils.getResponseResultS("修改成功");
    }

    @Override
    @Transactional
    public ResponseResult commodityCallPlanWebDelete(CommodityCallPlanWebDelDTO commodityCallPlanWebDelDTO) {
        final CommodityCallPlan commodityCallPlan = commodityCallPlanMapper.selectById(commodityCallPlanWebDelDTO.getId());
        Optional.ofNullable(commodityCallPlan).orElseThrow(() -> new SelfRuntimeException(ResponseResultUtils.getResponseResultF("话费礼包错误")));
        commodityCallPlan.setUpdateTime(DateUtil.date());
        commodityCallPlan.setIsDel(1);
        updateById(commodityCallPlan);
        return ResponseResultUtils.getResponseResultS("删除成功");
    }

    @Override
    @Transactional(readOnly = true)
    public ResponseResult getCallPlanSelectData() {
        List<CommodityCallPlan> data = commodityCallPlanMapper.selectList(new LambdaQueryWrapper<CommodityCallPlan>().eq(CommodityCallPlan::getIsDel, 0));
        return ResponseResultUtils.getResponseResultS("查询成功", data);
    }
}
